home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / gnu_oleo_1_2_2.lha / oleo-1.2.2 / ir.c < prev    next >
C/C++ Source or Header  |  1993-03-03  |  4KB  |  207 lines

  1. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12.  
  13. You should have received a copy of the GNU General Public License
  14. along with this software; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17.  
  18. #include "ir.h"
  19. int
  20. xx_IRencloses (r1, r2)
  21.      xx_IntRectangle r1;
  22.      xx_IntRectangle r2;
  23. {
  24.   return (xx_IRhits_point (r1, xx_IRllx (r2), xx_IRlly (r2))
  25.       && (!xx_IRw (r2) || !xx_IRh (r2)
  26.           || (xx_IRhits_point (r1, xx_IRurx (r2), xx_IRlly (r2))
  27.           && xx_IRhits_point (r1, xx_IRllx (r2), xx_IRury (r2))
  28.           && xx_IRhits_point (r1, xx_IRurx (r2), xx_IRury (r2)))));
  29. }
  30.  
  31. int
  32. xx_IRequiv (r1, r2)
  33.      xx_IntRectangle r1;
  34.      xx_IntRectangle r2;
  35. {
  36.   return (xx_IRllx (r2) == xx_IRllx (r1) &&
  37.       xx_IRlly (r2) == xx_IRlly (r1) &&
  38.       xx_IRurx (r2) == xx_IRurx (r1) &&
  39.       xx_IRury (r2) == xx_IRury (r1));
  40. }
  41.  
  42.  
  43. #define MAX(A,B)    ((A) > (B) ? (A) : (B))
  44. #define MIN(A,B)    ((A) < (B) ? (A) : (B))
  45. #define ROUND(F)     (int)((F) + 0.5)
  46. void
  47. xx_IRbound (r1, r2)
  48.      xx_IntRectangle r1;
  49.      xx_IntRectangle r2;
  50. {
  51.   int x, y;
  52.   int X, Y;
  53.  
  54.   x = MIN (xx_IRllx (r1), xx_IRllx (r2));
  55.   y = MIN (xx_IRlly (r1), xx_IRlly (r2));
  56.   X = MAX (xx_IRurx (r1), xx_IRurx (r2));
  57.   Y = MAX (xx_IRury (r1), xx_IRury (r2));
  58.  
  59.   r1->x = x;
  60.   r1->y = y;
  61.   r1->w = X - x + 1;
  62.   r1->h = Y - y + 1;
  63. }
  64.  
  65.  
  66. int
  67. xx_IRarea (r)
  68.      xx_IntRectangle r;
  69. {
  70.   return r->w * r->h;
  71. }
  72.  
  73.  
  74. int
  75. xx_IRhits_point (rect, x, y)
  76.      xx_IntRectangle rect;
  77.      int x;
  78.      int y;
  79. {
  80.   return (x >= rect->x
  81.       && y >= rect->y
  82.       && x < rect->x + rect->w
  83.       && y < rect->y + rect->h);
  84. }
  85.  
  86. void
  87. xx_IRclip (r1, r2)
  88.      xx_IntRectangle r1;
  89.      xx_IntRectangle r2;
  90. {
  91.   int x = MAX (r1->x, r2->x);
  92.   int y = MAX (r1->y, r2->y);
  93.   int X = MIN (xx_IRurx (r1), xx_IRurx (r2));
  94.   int Y = MIN (xx_IRury (r1), xx_IRury (r2));
  95.   int t;
  96.   r1->x = x;
  97.   r1->y = y;
  98.   t = X - x + 1;
  99.   if (t < 0)
  100.     r1->w = 0;
  101.   else
  102.     r1->w = t;
  103.   t = Y - y + 1;
  104.   if (t < 0)
  105.     r1->h = 0;
  106.   else
  107.     r1->h = t;
  108. }
  109.  
  110.  
  111.  
  112.  
  113. int
  114. xx_IRsubtract (outv, a, b)
  115.      xx_IntRectangle outv;
  116.      xx_IntRectangle a;
  117.      xx_IntRectangle b;
  118. {
  119.   struct xx_sIntRectangle arect;
  120.   struct xx_sIntRectangle brect;
  121.   int outp = 0;
  122.  
  123.   if (!(a->w && a->h))
  124.     return 0;
  125.   if (!(b->w && b->h))
  126.     {
  127.       *outv = *a;
  128.       return 1;
  129.     }
  130.  
  131.   arect = *a;
  132.   brect = *b;
  133.  
  134.   xx_IRclip (&brect, &arect);
  135.  
  136.   if (xx_IRhits_point (&arect,
  137.                xx_IRllx (&brect),
  138.                xx_IRlly (&brect)))
  139.     {
  140.       /*
  141.  
  142.      -----------------.
  143.     |        |//////brect
  144.     |        |////////|
  145.         |        |---------
  146.     |   I    |  II    |
  147.     |      |        |
  148.          -----------------  arect
  149.     */
  150.  
  151.       /* I  */
  152.       outv[outp] = arect;
  153.       outv[outp].w = xx_IRllx (&brect) - xx_IRllx (&arect);
  154.  
  155.       if (outv[outp].w)
  156.     ++outp;
  157.  
  158.       /* II */
  159.       outv[outp] = arect;
  160.       outv[outp].x = xx_IRllx (&brect);
  161.       outv[outp].w = xx_IRurx (&arect) - xx_IRllx (&brect) + 1;
  162.       outv[outp].h = xx_IRlly (&brect) - xx_IRlly (&arect);
  163.  
  164.       if (outv[outp].h)
  165.     ++outp;
  166.  
  167.       arect.w = xx_IRurx (&arect) - xx_IRllx (&brect) + 1;
  168.       arect.h = xx_IRury (&arect) - xx_IRlly (&brect) + 1;
  169.       arect.x = xx_IRllx (&brect);
  170.       arect.y = xx_IRlly (&brect);
  171.     }
  172.  
  173.   if (xx_IRhits_point (&arect,
  174.                xx_IRurx (&brect),
  175.                xx_IRury (&brect)))
  176.     {
  177.       /*
  178.               --------------
  179.                  |           I  | arect
  180.                  |--------------|
  181.                  |////////|     |
  182.                  |////////|  II |
  183.                   --------------
  184.              brect
  185.  
  186.  
  187.     */
  188.  
  189.       /* I */
  190.       outv[outp] = arect;
  191.       outv[outp].h = xx_IRury (&arect) - xx_IRury (&brect);
  192.       outv[outp].y = xx_IRury (&brect) + 1;
  193.       if (outv[outp].h)
  194.     ++outp;
  195.  
  196.       /* II */
  197.       outv[outp] = arect;
  198.       outv[outp].h = xx_IRury (&brect) - xx_IRlly (&arect) + 1;
  199.       outv[outp].w = xx_IRurx (&arect) - xx_IRurx (&brect);
  200.       outv[outp].x = xx_IRurx (&brect) + 1;
  201.  
  202.       if (outv[outp].w)
  203.     ++outp;
  204.     }
  205.   return outp;
  206. }
  207.